package com.ysj.demo.litepaltest;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ysj.demo.Data.News;

import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private TextView flagTv, contentTv;
    private Button searchBtn, addBtn, deleteBtn, updateBtn, updateTableBtn, deleteTableBtn, updateDbBtn;
    private ArrayList<News> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        initClick();

    }

    private void init() {
        flagTv = findViewById(R.id.is_flag_tv);
        contentTv = findViewById(R.id.content_tv);
        searchBtn = findViewById(R.id.search_btn);
        addBtn = findViewById(R.id.add_btn);
        deleteBtn = findViewById(R.id.delete_btn);
        updateBtn = findViewById(R.id.update_btn);
        updateTableBtn = findViewById(R.id.update_table_btn);
        deleteTableBtn = findViewById(R.id.delete_table_btn);
        updateDbBtn = findViewById(R.id.update_db_btn);

        list = new ArrayList<>();

        News one = new News();
        one.setTitle("今日头条大战腾讯");
        one.setContent("不可分享");
        one.setCommentCount(3);
        one.setPublishDate(new Date());
        list.add(one);

        News two = new News();
        two.setTitle("腾讯强推微视");
        two.setContent("广告铺天盖地");
        two.setCommentCount(5);
        two.setPublishDate(new Date());
        list.add(two);

        News three = new News();
        three.setTitle("币圈如股票");
        three.setContent("高风险，高回报");
        three.setCommentCount(4);
        three.setPublishDate(new Date());
        list.add(three);
    }

    private void initClick() {
        //查询数据库
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查询新闻表
                List<News> list = LitePal.where("commentCount < ?", "5").order("id").find(News.class);
                StringBuilder builder = new StringBuilder();
                for (News one : list) {
                    builder.append(one.toString()).append("\n");
                }
                contentTv.setText("list is " + list.size() + "\n" + builder.toString());

//                //查询新闻表
//                List<Comment> list = LitePal.where("id < ?", "5").order("id").find(Comment.class);
//                StringBuilder builder = new StringBuilder();
//                for (Comment one : list) {
//                    builder.append(one.toString()).append("\n");
//                }
//                contentTv.setText("list is " + list.size() + "\n" + builder.toString());
            }
        });
        //增加数据
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //增加新闻数据
                Random ra = new Random();
                int index = ra.nextInt(3);
                SQLiteDatabase db = Connector.getDatabase();
                boolean flag = list.get(index).save();
                flagTv.setText("save is " + flag);

//                //增加评论数据
//                Comment comment = new Comment();
//                comment.setContent("我日哦，what");
//                comment.setPublishDate(new Date());
//                boolean flag = comment.save();
//                flagTv.setText("save is " + flag);
            }
        });
        //删除数据
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除单条数据
//               int index = LitePal.delete(News.class,2);
//                flagTv.setText("delete is " + index);
                //删除表全部数据
                LitePal.deleteAll(News.class);
            }
        });
        //更新数据
        updateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("title", "中美贸易战");
                int index = LitePal.updateAll(News.class, values, "commentcount > ?", "3");
                flagTv.setText("修改的个数 update is " + index);
            }
        });
        //更新表
        updateTableBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = Connector.getDatabase();
            }
        });
        //删除表
        deleteTableBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        //更新数据库
        updateDbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = Connector.getDatabase();
            }
        });
    }

}
